//
//  JZPageContentView.m
//  eStudy(comprehensive)
//
//  Created by 飞猪 on 2018/10/26.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JZPageContentView.h"
#import "JZPageControllerConfig.h"
#import "JZPageCategoryView.h"

@interface JZPageContentView () <UIScrollViewDelegate, JZPageCategoryViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

#pragma mark - 记录
@property (nonatomic) NSArray<UIViewController *> *viewControllers;
@property (nonatomic, strong) JZPageControllerConfig *config;

@end

@implementation JZPageContentView

#pragma mark - LifeCycle
- (instancetype)initWithFrame:(CGRect)frame {
    return [self initWithFrame:frame config:[JZPageControllerConfig new] viewControllers:@[]];
}

//- (instancetype)initWithCoder:(NSCoder *)coder {
//    self = [super initWithCoder:coder];
//    if (self) {
//        [self createViews];
//    }
//    return self;
//}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self layoutSubview];
}

#pragma mark - APIs
- (instancetype)initWithFrame:(CGRect)frame
                       config:(JZPageControllerConfig *)config
              viewControllers:(NSArray<UIViewController *> *)controllers {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.viewControllers = controllers;
        self.config = config;
        
        [self createViews];
    }
    return self;
}


#pragma mark - CreateViews
- (void)createViews {
    self.backgroundColor = [UIColor whiteColor];
    
    // 0.
    [self addSubview:self.scrollView];
    
}

- (void)layoutSubview {
    
    self.scrollView.frame = self.bounds;

//    CGFloat x = 0;
//    CGFloat y = 0;
//    CGFloat w = self.bounds.size.width;
//    CGFloat h = self.bounds.size.height;
//
//    for (NSInteger index = 0; index < self.viewControllers.count; ++index) {
//        UIViewController *ctrl = [self.viewControllers objectAtIndex:index];
//
//        x = index * w;
//        [self.scrollView addSubview:ctrl.view];
//        ctrl.view.frame = CGRectMake(x, y, w, h);
//
//    }
    
    self.scrollView.contentSize = CGSizeMake(self.viewControllers.count * self.bounds.size.width, 0);
    [self.scrollView setContentOffset:CGPointMake(self.config.selectedIndex * self.bounds.size.width, 0) animated:NO];
    [self addSubView:self.config.selectedIndex];
}


#pragma mark - Lazyload

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.backgroundColor = [UIColor whiteColor];
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        scrollView.bounces = NO;
        
        _scrollView = scrollView;
    }
    return _scrollView;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageContentView:scrolledToIndex:)]) {
        
        NSInteger index = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);
        
        [self.delegate pageContentView:self scrolledToIndex:index];
        
        [self addSubView:index];
    }
    
     NSLog(@"+++++++++++");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageContentView:scrolledToIndex:)]) {

        NSInteger index = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);

        [self.delegate pageContentView:self scrolledToIndex:index];

        [self addSubView:index];
    }

    NSLog(@"------------");
    
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

    NSInteger index = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);
    
    [self addSubView:index];
    
    NSLog(@"===========");
}

- (void)addSubView:(NSInteger)index {
    
    UIViewController *ctrl = [self.viewControllers objectAtIndex:index];
    if (!ctrl.isViewLoaded) {
        UIView *subV = ctrl.view;
        subV.frame = CGRectMake(self.bounds.size.width * index, 0, self.bounds.size.width, self.bounds.size.height);
        
        [UIView transitionWithView:self.scrollView
                          duration:0.5
                           options:UIViewAnimationOptionShowHideTransitionViews
                        animations:^ { [self.scrollView addSubview:subV]; }
                        completion:nil];
        
    }
    
}

#pragma mark - JZPageCategoryViewDelegate
- (void)pageCategoryView:(JZPageCategoryView *)pageCategoryView clickedAtIndex:(NSInteger)index {
    
    if (index < 0 || index >= self.viewControllers.count) return;
    
    [self.scrollView setContentOffset:CGPointMake((self.scrollView.frame.size.width * index), 0) animated:YES];
    
}

@end
